import $http from "@/api/fetch";
import api from '@/api/api';
import { Encrypt } from "@/utils/sign";
/**
 * 登录，退出操作
 * 
 **/
const user = {
    state: {
        userName: 'none',
        userId: '',
        userInfo: {}
    },
    mutations: {
        LOGING_STATE: (state, params) => {

            localStorage.setItem('userInfo', JSON.stringify(params));
            localStorage.setItem("sessionKey", params.sessionKey)
            localStorage.setItem('userName', params.userName);
            localStorage.setItem('userId', params.id);
            localStorage.setItem('token', params.token);
            localStorage.setItem('roleName', params.roleName);
            state.userInfo = params;
            state.userName = params.merName;
            state.userId = params['id'];


        },
        LOGING_OUT: (state) => {

            localStorage.setItem('userInfo', "{}");
            localStorage.setItem('sessionKey', "");
            localStorage.setItem('userName', "");
            localStorage.setItem('token', "");
            localStorage.setItem('userId', "");
            localStorage.setItem('roleName', "");
            state.userInfo = {};
            state.userName = "";
            state.userId = "";
        }
    },
    actions: {
        qrLogin({ commit }, obj) { //登录异步操作

            return new Promise((resolve, reject) => {
                localStorage.setItem('userName', obj.merName);
                localStorage.setItem('userId', obj.id);
                commit('LOGING_STATE', obj);

                resolve();

            });
        },
        userLogin({ commit }, params) { //登录异步操作

            let userName = params.userName;
            let password;

            return new Promise((resolve, reject) => {
                $http.post(api.userLogin, { projectId: 1, code: params.code, userName: userName, password: params.password })
                    .then(res => {
                        if (res.status == 0) {

                            localStorage.setItem('userName', userName);
                            localStorage.setItem('userId', res.obj.id);
                            commit('LOGING_STATE', res.obj);
                        }
                        resolve(res);
                    }).catch(error => {
                        reject(error);
                    });
            });
        },
        loginOut({ commit }) {
            return new Promise((resolve, reject) => {
                $http.post(api.loginOut, {})
                    .then(res => {
                        if (res.status == 0) {
                            localStorage.setItem('userName', "");
                            localStorage.setItem('userId', "");
                            commit('LOGING_OUT');
                        }
                        resolve(res);
                    }).catch(error => {
                        reject(error);
                    });
            });
        },
        loginOutClear({ commit }) {
            return new Promise((resolve, reject) => {
                commit('LOGING_OUT');
                resolve();
            });
        }
    }
}

export default user